package customer.zspt.cn.customer.Widget.voice;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import customer.zspt.cn.customer.utils.AudioRecoderUtils;

/**
 * Created by Administrator on 2018/8/6 0006.
 */

public class AudioRecoderButton extends android.support.v7.widget.AppCompatButton {
    private AudioRecoderUtils audioRecoderUtils;
    private AudioTimeTipView tipView;
    //是否取消录音
    private boolean IsCancel = false;
    /**录音时间是否太短*/
    private boolean VoiceTime = false;
    public AudioRecoderButton(Context context) {
        this(context,null);
    }
    public AudioRecoderButton(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    public AudioRecoderButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setText("按住 说话");
        audioRecoderUtils = new AudioRecoderUtils();
        audioRecoderUtils.setOnAudioStatusUpdateListener(new AudioRecoderUtils.OnAudioStatusUpdateListener() {
            @Override
            public void onUpdate(double db, long time) {
                if(mAudioStatusUpdateListener!=null)
                {
                    mAudioStatusUpdateListener.onUpdate(db,time);
                }
            }
            @Override
            public void onStop(String filePath,long time) {
                if(mAudioStatusUpdateListener!=null)
                {
                    mAudioStatusUpdateListener.onStop(filePath,time);
                }
            }
            @Override
            public void onTimeShort() {
                if(mAudioStatusUpdateListener!=null){
                    mAudioStatusUpdateListener.onShort();
                }
            }
        });
    }
    int y = 0;
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                this.setText("松开 发送");
                y = (int) event.getRawY();
                audioRecoderUtils.startRecord();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) event.getRawY();
                Log.w("位置",(moveY-y)+"");
                //向下滑动
                 if(moveY - y>=-150){
                     IsCancel = false;
                     if(mAudioStatusUpdateListener!=null){
                         mAudioStatusUpdateListener.Recoding();
                     }
                 }
                 //向上滑动
                 else if(moveY-y<-50){
                     IsCancel = true;
                     if(mAudioStatusUpdateListener!=null){
                         mAudioStatusUpdateListener.cancel();
                     }
                 }
                break;
            case MotionEvent.ACTION_UP:
                this.setText("按住 说话");
                if(!IsCancel){
                   audioRecoderUtils.stopRecord();
                }else{
                    audioRecoderUtils.cancelRecord();
                    if(mAudioStatusUpdateListener!=null){
                            mAudioStatusUpdateListener.OnGoneTip();
                    }
                }
                break;
        }
        return super.dispatchTouchEvent(event);
    }
    private OnAudioStatusUpdateListener mAudioStatusUpdateListener;
    /**设置信息监听*/
    public void getAudioStatusInfoLinstener(OnAudioStatusUpdateListener mAudioStatusUpdateListener)
    {
        this.mAudioStatusUpdateListener = mAudioStatusUpdateListener;
    }
    /**获取录音的信息*/
    public interface OnAudioStatusUpdateListener {
        public void onUpdate(double db,long time);
        public void onStop(String filePath, long time);
        //录音时间太短
        public void onShort();
        /**取消录音*/
        public void cancel();
        /**继续录音*/
        public void Recoding();
        public void OnGoneTip();
    }
    public void setTipView(AudioTimeTipView tipView){
        this.tipView = tipView;
    }
}
